package com;



import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.MainAction;
import com.admin.Company;
import com.admin.DepartmentAction;
import com.admin.Menu;
import com.admin.Role;

import com.admin.User;
import com.bg.BGMapper;
import com.bg.DhlMenu;
import com.bg.Dhlt;

public class MainAction extends DepartmentAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 6951592753374448555L;
	private List<Menu> menus;
	private Company company;
	private Role role;
	
	private List<DhlMenu> dhlMenus;
	private List<Dhlt> dhlts;
	private int Dhid;
	
	@SuppressWarnings({ "deprecation" })
	public String loginmain() throws IOException {
		User loginuser = (User)session.get(Constants.USER);
		if(loginuser==null) {
			message="登录超时！";
			return "failure";
		}
		 
		
		try {
			Map<String,String> pharm=new HashMap<String,String>();
			pharm.put("Userid", loginuser.getUserid()+"");
			role=getRolebyuser(pharm);
			if(role==null) {
				message="账号异常！";
				return "failure";
			}
			session.put(Constants.USER_ROLES, role);
			pharm.remove("Userid");
			pharm.put("Roleid", role.getRoleid()+"");
			com.CreateNamePicture.generateImg(loginuser.getUsercode(), request.getRealPath("/") +"photo", loginuser.getUsername());
			
			if(role.getRoleid()!=3) {
				menus=getmenusbyrole(pharm);
				return "sys";
			}
			
			
			} finally {
			
		}
		return "success";
	}

	
	
	
	@SuppressWarnings({ "rawtypes",  "unchecked" })
	public String bgmain() {
		
		SqlSession sqlSession = getSqlSession();
		
		try {
			BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
			dhlts=bgmmp.selectdhlts();
			if(Dhid==0) {
				Dhid=1;
			}
			
			for(int a=0;a<dhlts.size();a++) {
				if(dhlts.get(a).getDhid()==Dhid) {
					dhlts.get(a).setChecked(1);
				}
			}
//			for(int a=0;a<dhlts.size();a++) {
//				if(dhlts.get(a).getChecked()==1) {
					Map map = new HashMap();
					map.put("Dhid", Dhid);
					List<DhlMenu> list=bgmmp.selectdhlMenus(map);
					List<DhlMenu> zcd = new ArrayList();
					dhlMenus = new ArrayList();
					for(int b=0;b<list.size();b++) {
						if(list.get(b).getZCD()==1) {
							list.get(b).setZmenu(new ArrayList());
							dhlMenus.add(list.get(b));
						}else {
							zcd.add(list.get(b));
						}
					}
					
					for(int c=0;c<zcd.size();c++) {
						
					   for(int d=0;d<dhlMenus.size();d++) {
							if(dhlMenus.get(d).getDMid()==zcd.get(c).getSJMid()) {
								dhlMenus.get(d).getZmenu().add(zcd.get(c));
							}
						}
					}
//				}
//			}
		
		} finally {
			sqlSession.close();
		}
		
		
	 return SUCCESS;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void getmenus() {
      SqlSession sqlSession = getSqlSession();
		
		try {
			BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
			Map map = new HashMap();
			map.put("Dhid", Dhid);
			List<DhlMenu> list=bgmmp.selectdhlMenus(map);
			List<DhlMenu> zcd = new ArrayList();
			dhlMenus = new ArrayList();
			for(int b=0;b<list.size();b++) {
				if(list.get(b).getZCD()==1) {
					list.get(b).setZmenu(new ArrayList());
					dhlMenus.add(list.get(b));
				}else {
					zcd.add(list.get(b));
				}
			}
			
			for(int c=0;c<zcd.size();c++) {
			   for(int d=0;d<dhlMenus.size();d++) {
					if(dhlMenus.get(d).getDMid()==zcd.get(c).getSJMid()) {
						dhlMenus.get(d).getZmenu().add(zcd.get(c));
					}
				}
			}
			
			
		    }finally {
				sqlSession.close();
			}
			ajaxJson(dhlMenus);
	}
	
   public void clearBeizhu() {
	   User loginuser = (User)session.get(Constants.USER); 
	   loginuser.setBeizhu("");
	   session.put(Constants.USER, loginuser);
	   
   }

 
	private List<Menu> getmenusbyrole(Map<String, String> pharm) {
		// TODO Auto-generated method stub
		return selectMenus(pharm);
	}



	private Role getRolebyuser(Map<String, String> pharm) {
		// TODO Auto-generated method stub
		return selectRole(pharm);
	}



	private Company getCompanybyuser(Map<String, String> pharm) {
		// TODO Auto-generated method stub
		return selectCompany(pharm);
	}



	public List<Menu> getMenus() {
		return menus;
	}



	public void setMenus(List<Menu> menus) {
		this.menus = menus;
	}



	public Company getCompany() {
		return company;
	}



	public void setCompany(Company company) {
		this.company = company;
	}



	public Role getRole() {
		return role;
	}



	public void setRole(Role role) {
		this.role = role;
	}




	public List<DhlMenu> getDhlMenus() {
		return dhlMenus;
	}




	public void setDhlMenus(List<DhlMenu> dhlMenus) {
		this.dhlMenus = dhlMenus;
	}




	public List<Dhlt> getDhlts() {
		return dhlts;
	}




	public void setDhlts(List<Dhlt> dhlts) {
		this.dhlts = dhlts;
	}




	public int getDhid() {
		return Dhid;
	}




	public void setDhid(int dhid) {
		Dhid = dhid;
	}
	
	
}